import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/ent',
    name: 'ent',
    component: () => import('../views/ent.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 全局的路由导航守卫
router.beforeEach((to,from,next)=>{
  // 判断是否登录
  var token=localStorage.getItem("token");
  if(token){//登录
    if(to.path==='/login'){
      next('/home')
    }else{
      next()
    }
   
  }
  else{//未登录
    if(to.path==='/login'){
      next()
    }else{
      next("/login")
    }
   
  }
})
export default router
